fix: use timezone-aware datetimes in cache _time_since_modification#2130
fix: use timezone-aware datetimes in cache _time_since_modification#2130Acuspeedster wants to merge 8 commits intooscal-compass:developfrom
Conversation
Signed-off-by: Acuspeedster <arnavrajsingh@gmail.com>
|
@degenaro @vikas-agarwal76 |
|
@degenaro I think you have already gone through this code change... this pr just needs to be merged. |
Signed-off-by: Acuspeedster <arnavrajsingh@gmail.com>
|
@degenaro @vikas-agarwal76 reminder |
degenaro
left a comment
There was a problem hiding this comment.
This is attempting to standardize time to utc.
Questions:
- what callers are affected?
- is similar change needed elsewhere in the code base for consistency?
- will this have any backward compatibility issues?
- will this change have any user facing impact?
|
Hey @degenaro
Net: this is a low-risk bug fix that standardizes cache age calculations to UTC-aware datetimes, consistent with prior timezone-hardening work. |
|
@Acuspeedster Thx. Related to testing, I see for coverage: While we are here, can we improve the coverage? |
Signed-off-by: Acuspeedster <arnavrajsingh@gmail.com>
Signed-off-by: Acuspeedster <arnavrajsingh@gmail.com>
|
Hey @degenaro I added a focused unit test for the timezone fix:
This test explicitly validates that Test run: |
Use timezone-aware
datetimeobjects inFetcherBase._time_since_modificationincache.pyto prevent incorrect cache expiry calculations in DST-observing environments.Changes:
_time_since_modificationreplacedatetime.datetime.fromtimestamp(mtime)anddatetime.datetime.now()(both naive) withdatetime.datetime.fromtimestamp(mtime, tz=datetime.timezone.utc)anddatetime.datetime.now(datetime.timezone.utc)(both UTC-aware)Types of changes
develop->main)Quality assurance (all should be covered).
Summary
FetcherBase._time_since_modificationcalculates how long ago a cached file was last modified, and_is_staleuses this value to decide whether to re-fetch a remote OSCAL resource.The bug:
datetime.datetime.fromtimestamp(mtime)anddatetime.datetime.now()both return naive datetime objects (notzinfo). On systems in DST-observing timezones, the OS filesystem timestamp is stored in UTC, butfromtimestampconverts it to local wall-clock time. When a DST transition occurs between the time the file was written and the time_is_staleis called, the two naive datetimes can be in different UTC offsets. The subtraction then silently produces a timedelta that is off by exactly one hour causing a cache hit to be treated as stale (or a stale cache to be treated as fresh) for the duration around the DST boundary.The fix: pass
tz=datetime.timezone.utcto both calls so that both operands are timezone-aware and refer to the same UTC reference, making the subtraction always correct regardless of local timezone or DST transitions.This is the same class of datetime handling issue that was fixed in
model_ageby #2110.Key links:
Before you merge